//
//  HMVisitorView.swift
//  Weibo02
//
//  Created by teacher on 16/4/16.
//  Copyright © 2016年 teacher. All rights reserved.
//

import UIKit

import SnapKit

@objc protocol VisitorViewDelegate: NSObjectProtocol{
    //必须实现的方法
    func loginButtonClick()
    func registerButtonClick()
    
}

class HMVisitorView: UIView {
    
    weak var delegate:VisitorViewDelegate?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
        beginRomate()
        
        
        
      
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置不同visitor的图片
    func  setimageAndmessage(image: String?,message: String?) {
        if let im = image,me = message {
            self.iconView.image = UIImage(named: im)
            self.messageLabel.text = me
        }else{
            circleView.hidden = false
        }
    }
    
    // 设置视图
    private func setupUI(){
        //        UIColor(red: 237/255.0, green: 237/255.0, blue: 237/255.0, alpha: 1)
        // UIColor(white: 237/255.0, alpha: 1) 与上面的效果相同
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        // 1. 添加控件
        addSubview(circleView)
        addSubview(maskIconView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
//        // 2. 添加约束 --> 系统原生的约束
//        iconView.translatesAutoresizingMaskIntoConstraints = false
//        /**
//         参数1：要添加约束的对象
//         参数2：要添加哪个约束
//         参数3：约束与约束之前的关系 一般都用 equal
//         参数4：参照约束的对象
//         参数5：要约束参照对象的那个约束
//         参数6：乘积
//         参数7：偏移量
//         */
//        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.center.x)
            make.centerY.equalTo(self)
        }

        // 圆圈
        circleView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 0))
        
        // 消息label
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 15))
        
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: .Equal, toItem: messageLabel, attribute: .Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 15))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        // 登录按钮
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: .Equal, toItem: messageLabel, attribute: .Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: registerButton, attribute: .Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        // 透明度
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
    }
    
    
    // MARK: - 懒加载控件
    
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    
    
    
    //旋转函数
    func beginRomate() -> () {
        circleView.hidden = true
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        
        animation.duration = 20.0
        animation.repeatCount = MAXFLOAT
        
        animation.removedOnCompletion = false
        
        self.circleView.layer.addAnimation(animation, forKey: nil)
    }
    
    
    private lazy var messageLabel: UILabel = {
        // UILabel(textColor: UIColorxx,  fontSize: 16)
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 16, preferredMaxLayoutWidth: 215)
        label.text = "哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈"
        label.textAlignment = .Center
        
        //        label.textColor = UIColor.darkGrayColor()
        //        label.font = UIFont.systemFontOfSize(16)
        
        // 以下两行代码已抽取到 UILabel 的 extension 的便利构造函数里面去
        //        // 设置多行模式
        //        label.numberOfLines = 0
        //        // 设置最大布局的宽度
        //        label.preferredMaxLayoutWidth = 200
        return label
    }()
    
    // 注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton(textColor: UIColor.orangeColor(), fontSize: 14)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "clickRegisterButton", forControlEvents: .TouchUpInside)
        return button
    }()
    
    func clickRegisterButton() {
        delegate?.registerButtonClick()
    }
    
    // 登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton(textColor: UIColor.darkGrayColor(), fontSize: 14)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "clickLoginButton", forControlEvents: .TouchUpInside)
        return button
    }()
    
    func clickLoginButton()  {
        delegate?.loginButtonClick()
    }
    
    // 半透明背景
    private lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
}
